package com.cheyunkeji.er.activity.evaluate;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.evaluate.EvaluateDetailResult2;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.cheyunkeji.er.view.evaluate.BCSMCheckItemView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;

public class EngineCabinCheckBCSMActivity extends BaseActivity {

    private static final String TAG = EngineCabinCheckBCSMActivity.class.getSimpleName();

    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.bcsm_26)
    BCSMCheckItemView bcsm26;
    @BindView(R.id.bcsm_27)
    BCSMCheckItemView bcsm27;
    @BindView(R.id.bcsm_28)
    BCSMCheckItemView bcsm28;
    @BindView(R.id.bcsm_29)
    BCSMCheckItemView bcsm29;
    @BindView(R.id.bcsm_30)
    BCSMCheckItemView bcsm30;
    @BindView(R.id.bcsm_31)
    BCSMCheckItemView bcsm31;
    @BindView(R.id.bcsm_32)
    BCSMCheckItemView bcsm32;
    @BindView(R.id.bcsm_33)
    BCSMCheckItemView bcsm33;
    @BindView(R.id.bcsm_34)
    BCSMCheckItemView bcsm34;
    @BindView(R.id.bcsm_35)
    BCSMCheckItemView bcsm35;
    @BindView(R.id.bcsm_36)
    BCSMCheckItemView bcsm36;
    @BindView(R.id.bcsm_37)
    BCSMCheckItemView bcsm37;
    @BindView(R.id.bcsm_38)
    BCSMCheckItemView bcsm38;
    @BindView(R.id.bcsm_39)
    BCSMCheckItemView bcsm39;
    @BindView(R.id.bcsm_40)
    BCSMCheckItemView bcsm40;
    @BindView(R.id.bcsm_41)
    BCSMCheckItemView bcsm41;
    @BindView(R.id.bcsm_42)
    BCSMCheckItemView bcsm42;
    @BindView(R.id.bcsm_43)
    BCSMCheckItemView bcsm43;
    @BindView(R.id.bcsm_44)
    BCSMCheckItemView bcsm44;
    @BindView(R.id.bcsm_45)
    BCSMCheckItemView bcsm45;
    @BindView(R.id.bcsm_46)
    BCSMCheckItemView bcsm46;
    @BindView(R.id.bcsm_47)
    BCSMCheckItemView bcsm47;
    @BindView(R.id.bcsm_48)
    BCSMCheckItemView bcsm48;
    @BindView(R.id.bcsm_49)
    BCSMCheckItemView bcsm49;
    @BindView(R.id.bcsm_50)
    BCSMCheckItemView bcsm50;
    @BindView(R.id.bcsm_51)
    BCSMCheckItemView bcsm51;
    @BindView(R.id.bcsm_52)
    BCSMCheckItemView bcsm52;
    @BindView(R.id.bcsm_53)
    BCSMCheckItemView bcsm53;
    @BindView(R.id.bcsm_54)
    BCSMCheckItemView bcsm54;
    @BindView(R.id.bcsm_55)
    BCSMCheckItemView bcsm55;
    @BindView(R.id.bcsm_56)
    BCSMCheckItemView bcsm56;
    @BindView(R.id.bcsm_57)
    BCSMCheckItemView bcsm57;
    @BindView(R.id.bcsm_58)
    BCSMCheckItemView bcsm58;
    @BindView(R.id.bcsm_59)
    BCSMCheckItemView bcsm59;
    @BindView(R.id.bcsm_60)
    BCSMCheckItemView bcsm60;
    @BindView(R.id.bcsm_61)
    BCSMCheckItemView bcsm61;
    @BindView(R.id.bcsm_62)
    BCSMCheckItemView bcsm62;
    @BindView(R.id.bcsm_63)
    BCSMCheckItemView bcsm63;
    @BindView(R.id.bcsm_64)
    BCSMCheckItemView bcsm64;
    @BindView(R.id.bcsm_65)
    BCSMCheckItemView bcsm65;
    @BindView(R.id.bcsm_66)
    BCSMCheckItemView bcsm66;
    @BindView(R.id.bcsm_67)
    BCSMCheckItemView bcsm67;
    @BindView(R.id.bcsm_68)
    BCSMCheckItemView bcsm68;
    @BindView(R.id.bcsm_69)
    BCSMCheckItemView bcsm69;
    @BindView(R.id.bcsm_70)
    BCSMCheckItemView bcsm70;
    @BindView(R.id.bcsm_71)
    BCSMCheckItemView bcsm71;
    @BindView(R.id.bcsm_72)
    BCSMCheckItemView bcsm72;
    @BindView(R.id.bcsm_73)
    BCSMCheckItemView bcsm73;
    @BindView(R.id.bcsm_74)
    BCSMCheckItemView bcsm74;
    @BindView(R.id.bcsm_75)
    BCSMCheckItemView bcsm75;
    @BindView(R.id.bcsm_76)
    BCSMCheckItemView bcsm76;
    @BindView(R.id.bcsm_77)
    BCSMCheckItemView bcsm77;
    @BindView(R.id.bcsm_78)
    BCSMCheckItemView bcsm78;
    @BindView(R.id.bcsm_79)
    BCSMCheckItemView bcsm79;
    @BindView(R.id.bcsm_80)
    BCSMCheckItemView bcsm80;
    @BindView(R.id.bcsm_81)
    BCSMCheckItemView bcsm81;
    @BindView(R.id.bcsm_82)
    BCSMCheckItemView bcsm82;
    @BindView(R.id.bcsm_83)
    BCSMCheckItemView bcsm83;
    @BindView(R.id.bcsm_84)
    BCSMCheckItemView bcsm84;
    @BindView(R.id.activity_cabin_check_bcsm)
    LinearLayout activityCabinCheckBcsm;


    private Map<Integer, BCSMCheckItemView> itemViewList = null;

    private List<EvaluateDetailResult2.DetectionBean.EncompMarkBean> encompBeanList = null;

    @Override
    protected void init() {

        setContentView(R.layout.er_activity_cabin_check_bcsm);
        ButterKnife.bind(this);

        itemViewList = new HashMap<>();
        itemViewList.put(26,bcsm26);
        itemViewList.put(27,bcsm27);
        itemViewList.put(28,bcsm28);
        itemViewList.put(29,bcsm29);
        itemViewList.put(30,bcsm30);
        itemViewList.put(31,bcsm31);
        itemViewList.put(32,bcsm32);
        itemViewList.put(33,bcsm33);
        itemViewList.put(34,bcsm34);
        itemViewList.put(35,bcsm35);
        itemViewList.put(36,bcsm36);
        itemViewList.put(37,bcsm37);
        itemViewList.put(38,bcsm38);
        itemViewList.put(39,bcsm39);
        itemViewList.put(40,bcsm40);
        itemViewList.put(41,bcsm42);
        itemViewList.put(43,bcsm43);
        itemViewList.put(44,bcsm44);
        itemViewList.put(45,bcsm45);
        itemViewList.put(46,bcsm46);
        itemViewList.put(47,bcsm47);
        itemViewList.put(48,bcsm48);
        itemViewList.put(49,bcsm49);
        itemViewList.put(50,bcsm50);
        itemViewList.put(51,bcsm51);
        itemViewList.put(52,bcsm52);
        itemViewList.put(53,bcsm53);
        itemViewList.put(54,bcsm54);
        itemViewList.put(55,bcsm55);
        itemViewList.put(56,bcsm56);
        itemViewList.put(57,bcsm57);
        itemViewList.put(58,bcsm58);
        itemViewList.put(59,bcsm59);
        itemViewList.put(60,bcsm60);
        itemViewList.put(61,bcsm61);
        itemViewList.put(62,bcsm62);
        itemViewList.put(63,bcsm63);
        itemViewList.put(64,bcsm64);
        itemViewList.put(65,bcsm65);
        itemViewList.put(66,bcsm66);
        itemViewList.put(67,bcsm67);
        itemViewList.put(68,bcsm68);
        itemViewList.put(69,bcsm69);
        itemViewList.put(70,bcsm70);
        itemViewList.put(71,bcsm71);
        itemViewList.put(72,bcsm72);
        itemViewList.put(73,bcsm73);
        itemViewList.put(74,bcsm74);
        itemViewList.put(75,bcsm75);
        itemViewList.put(76,bcsm76);
        itemViewList.put(77,bcsm77);
        itemViewList.put(78,bcsm78);
        itemViewList.put(79,bcsm79);
        itemViewList.put(80,bcsm80);
        itemViewList.put(81,bcsm81);
        itemViewList.put(82,bcsm82);
        itemViewList.put(83,bcsm83);
        itemViewList.put(84,bcsm84);

    }

    @Override
    protected void initView() {

        vTopbar.setLeftBack();
        vTopbar.setRight("保存", -1, this);
        vTopbar.setTitle("发动机舱补充说明");

        if (null != MyApplication.getInstance().getEvaluateResult().getDetection() && null != MyApplication.getInstance().getEvaluateResult().getDetection().getEncomp_mark()) {

            encompBeanList = MyApplication.getInstance().getEvaluateResult().getDetection().getEncomp_mark();

            if (encompBeanList.size() > 0) {

                displayInfo();

            }
        }
    }


    private void displayInfo() {

        for (EvaluateDetailResult2.DetectionBean.EncompMarkBean bean : encompBeanList) {

            itemViewList.get(bean.getId()).setCheckedIndex(bean.getStatus()-1);
            itemViewList.get(bean.getId()).setTag(bean);
        }

    }


    @Override
    protected void initData() {


    }


    @Override
    public void onClick(View v) {
        super.onClick(v);

        if(encompBeanList == null){
            encompBeanList = new ArrayList<>();
        }else{
            encompBeanList.clear();
        }

        if(v.getId() == R.id.vRight){

            for(int  index : itemViewList.keySet() ){
                if(3 != itemViewList.get(index).getCurrentCheckedIndex()){

                    encompBeanList.add(new EvaluateDetailResult2.DetectionBean.EncompMarkBean(index,itemViewList.get(index).getCurrentCheckedIndex() + 1));
                }
            }

            String myData = new StringBuilder("{\"encomp_mark\":").append(new Gson().toJson(encompBeanList)).append("}").toString();

            Log.e(TAG, "onClick: " + new Gson().toJson(encompBeanList));

            HashMap<String, String> params = new HashMap<>();
//            params.put("authkey", UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
//            params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(), ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
            params.put("aid",MyApplication.getInstance().getEvaluateResult().getAid());

            params.put("detection",myData);

            ApiClient.postForm(HttpConstants.INSERT_ARCHIVE_INFO, params, new RespCallback<EvaluateDetailResult2>() {

                @Override
                protected void onRequestFailed(String msg) {
                    SToast.show(msg);
                }

                @Override
                protected void onSuccess(EvaluateDetailResult2 data) {
                    if (null != data) {
                        MyApplication.getInstance().setEvaluateResult(data);
                        SToast.show("保存成功");
                        MyApplication.getInstance().removeActivity(EngineCabinCheckBCSMActivity.class);
                        finish();
                    }
                }

                @Override
                public void onBefore(Request request, int id) {
                    showDialog();
                }

                @Override
                public void onAfter(int id) {
                    dismissDialog();
                }

            });

        }
    }



}
